swipe: Add function to retrieve the current velocity
authorCarlos Garnacho <carlosg@gnome.org>
Tue, 8 Apr 2014 19:19:46 +0000 (21:19 +0200)
committerCarlos Garnacho <carlosg@gnome.org>
Fri, 23 May 2014 17:54:26 +0000 (19:54 +0200)
This can be used to fetch the current velocity on update(), as opposed
to swipe() which happens after the sequence is finished.

gtk/gtkgestureswipe.c
gtk/gtkgestureswipe.h

index d04504a276519045fb6fcb9779ddd41193bc95fa..e4f6e29dfa61a19aab54cff3c52ccc06e8b65036 100644 (file)
@@ -109,13 +109,18 @@ _gtk_gesture_swipe_calculate_velocity (GtkGestureSwipe *gesture,
                                        gdouble         *velocity_y)
 {
   GtkGestureSwipePrivate *priv;
+  GdkEventSequence *sequence;
+  guint32 evtime, diff_time;
   EventData *start, *end;
   gdouble diff_x, diff_y;
-  guint32 diff_time;
 
   priv = gtk_gesture_swipe_get_instance_private (gesture);
   *velocity_x = *velocity_y = 0;
 
+  sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
+  gtk_gesture_get_last_update_time (GTK_GESTURE (gesture), sequence, &evtime);
+  _gtk_gesture_swipe_clear_backlog (gesture, evtime);
+
   if (priv->events->len == 0)
     return;
 
@@ -141,14 +146,14 @@ gtk_gesture_swipe_end (GtkGesture       *gesture,
   GtkGestureSwipe *swipe = GTK_GESTURE_SWIPE (gesture);
   GtkGestureSwipePrivate *priv;
   gdouble velocity_x, velocity_y;
-  guint32 evtime;
+  GdkEventSequence *seq;
+
+  seq = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
 
-  if (gtk_gesture_get_sequence_state (gesture, sequence) == GTK_EVENT_SEQUENCE_DENIED)
+  if (gtk_gesture_get_sequence_state (gesture, seq) == GTK_EVENT_SEQUENCE_DENIED)
     return;
 
   priv = gtk_gesture_swipe_get_instance_private (swipe);
-  gtk_gesture_get_last_update_time (gesture, sequence, &evtime);
-  _gtk_gesture_swipe_clear_backlog (swipe, evtime);
   _gtk_gesture_swipe_calculate_velocity (swipe, &velocity_x, &velocity_y);
   g_signal_emit (gesture, signals[SWIPE], 0, velocity_x, velocity_y);
 
@@ -202,3 +207,25 @@ gtk_gesture_swipe_new (GtkWidget *widget)
                        "widget", widget,
                        NULL);
 }
+
+gboolean
+gtk_gesture_swipe_get_velocity (GtkGestureSwipe *gesture,
+                                gdouble         *velocity_x,
+                                gdouble         *velocity_y)
+{
+  gdouble vel_x, vel_y;
+
+  g_return_val_if_fail (GTK_IS_GESTURE (gesture), NULL);
+
+  if (!gtk_gesture_is_recognized (GTK_GESTURE (gesture)))
+    return FALSE;
+
+  _gtk_gesture_swipe_calculate_velocity (gesture, &vel_x, &vel_y);
+
+  if (velocity_x)
+    *velocity_x = vel_x;
+  if (velocity_y)
+    *velocity_y = vel_y;
+
+  return TRUE;
+}
index b8564b0f18bacb25a7b5d64328346c2fafffb700..fec6132c5fd5ddbe4653a2a066d7e1d1f186e86b 100644 (file)
@@ -62,6 +62,11 @@ GType        gtk_gesture_swipe_get_type  (void) G_GNUC_CONST;
 GDK_AVAILABLE_IN_3_14
 GtkGesture * gtk_gesture_swipe_new       (GtkWidget *widget);
 
+GDK_AVAILABLE_IN_3_14
+gboolean     gtk_gesture_swipe_get_velocity (GtkGestureSwipe *gesture,
+                                             gdouble         *velocity_x,
+                                             gdouble         *velocity_y);
+
 G_END_DECLS
 
 #endif /* __GTK_GESTURE_SWIPE_H__ */